
export function getGridWidth(totalWidth:number,widthGridNum:number,waterTank:number){
    let gridWidth = ((totalWidth - waterTank) / widthGridNum) - waterTank;
    return gridWidth;
}

export function getGridHeight(totalHeight:number,heightGridNum:number,waterTank:number){
    let gridHeight = ((totalHeight - waterTank) / heightGridNum) - waterTank;
    return gridHeight;
}

export function getTop( unitTop:number, gridHeight:number, waterTank:number){
    let top = unitTop * gridHeight + ((unitTop+1) * waterTank) ;
    return top;
}
export function getLeft( unitLeft:number,gridWidth:number, waterTank:number){
    let left = unitLeft * gridWidth + ((unitLeft+1) * waterTank);
    return left;
}

export function getHeight( unitHeight:number, gridHeight:number, waterTank:number){
    let top = unitHeight * gridHeight + ((unitHeight-1) * waterTank) ;
    return top;
}
export function getWidth( unitWidth:number,gridWidth:number, waterTank:number){
    let left = unitWidth * gridWidth + ((unitWidth-1) * waterTank);
    return left;
}

export function getFinalLocationByAbsoluteLocation(left:number, top:number,
    playgroundWidth:number,
    playgroundHeight:number,
    width:number,
    height:number,
    gridWidth:number,gridHeight:number,waterTank:number){
    let unitLeft = 0,
        unitTop = 0,
        unitWidth = 0,
        unitHeight = 0;
    if(left < 0){
        unitLeft = 0;
        left = 0;
    }else{
        unitLeft = Math.round(left / (gridWidth+waterTank));
        left = unitLeft * (gridWidth+waterTank)+waterTank;
    }
    if(top <0){
        unitTop = 0;
        top = 0;
    }else{
        unitTop = Math.round(top / (gridHeight+waterTank));
        top = unitTop *  (gridHeight+waterTank)+waterTank;
    }
    if (top + height > playgroundHeight) {
       top = playgroundHeight - height - waterTank;

    }
    if (left + width > playgroundWidth) {
        left = playgroundWidth - width - waterTank;
    }
    unitWidth =  Math.round(width / ((gridWidth-1)+waterTank));
    unitHeight = Math.round(height / ((gridHeight-1)+waterTank));
    console.log(unitWidth,unitHeight);
    width = (unitWidth*gridWidth) + ((unitWidth-1)*waterTank);
    height = (unitHeight*gridHeight) + ((unitHeight-1)*waterTank);


    return {
        unitLeft,
        unitTop,
        unitWidth,
        unitHeight,
        left,
        top,
        width,
        height
    }
}